package com.ants.boot.wx.service.impl;

import com.ants.boot.core.security.user.LoginUser;
import com.ants.boot.model.BaseModel;
import com.ants.boot.core.security.utils.SecurityUtils;
import com.ants.boot.wx.dao.WxBannerMapper;
import com.ants.boot.wx.domain.WxBanner;
import com.ants.boot.wx.dto.wxbanner.QueryPageInfoDto;
import com.ants.boot.wx.service.WxBannerService;
import com.ants.boot.wx.vo.wxbanner.AddWxBannerDto;
import com.ants.boot.wx.vo.wxbanner.ChangeStatusDto;
import com.ants.boot.wx.vo.wxbanner.UpdateWxBannerDto;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 蚂蚁会花呗
 * @date 2022/5/23 16:39
 */
@Service
public class WxBannerServiceImpl extends ServiceImpl<WxBannerMapper, WxBanner> implements WxBannerService {

    @Override
    public IPage<WxBanner> pageInfo(QueryPageInfoDto dto) {
        Page<WxBanner> page = new Page<>(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<WxBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .orderByDesc(BaseModel::getCreateTime);
        if (StringUtils.isNotBlank(dto.getStatus())){
            queryWrapper.and(wrapper ->wrapper.lambda().eq(WxBanner::getStatus,dto.getStatus()));
        }
        return this.page(page,queryWrapper);
    }

    @Override
    @Transactional
    public boolean addBanner(AddWxBannerDto dto) {
        WxBanner wxBanner = new WxBanner();
        wxBanner.setUrl(dto.getUrl());
        wxBanner.setOperate(SecurityUtils.getLoginUser().getUsername());
        wxBanner.setStatus(dto.getStatus());
        return this.save(wxBanner);
    }

    @Override
    @Transactional
    public boolean updateBanner(UpdateWxBannerDto dto) {
        UpdateWrapper<WxBanner> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(WxBanner::getId,dto.getId())
                .set(WxBanner::getUrl,dto.getUrl())
                .set(WxBanner::getOperate,SecurityUtils.getLoginUser().getUsername())
                .set(BaseModel::getStatus,dto.getStatus());
        return this.update(updateWrapper);
    }

    @Override
    @Transactional
    public boolean delBanner(List<Integer> ids) {
        return this.removeByIds(ids);
    }

    @Override
    @Transactional
    public boolean changeStatus(ChangeStatusDto dto) {
        UpdateWrapper<WxBanner> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda()
                .eq(WxBanner::getId,dto.getId())
                .set(BaseModel::getStatus,dto.getStatus())
                .set(WxBanner::getOperate,SecurityUtils.getLoginUser().getUsername());
        return this.update(updateWrapper);
    }

    @Override
    public List<String> listBanner() {
        QueryWrapper<WxBanner> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .orderByDesc(BaseModel::getCreateTime);
        IPage<WxBanner> page = this.page(new Page<>(1,3),queryWrapper);
        if (!page.getRecords().isEmpty()){
            List<String> result = page.getRecords().stream().map(WxBanner::getUrl).collect(Collectors.toList());
            return result;
        }
        return new ArrayList<>();
    }
}
